const { Controller } = require('egg')

const rule = {
	username: 'string',
	password: 'string',
	invitationCode: 'string',
}

/**
 * @controller register 注册
 */
class RegisterController extends Controller {
	/**
	 * @summary 注册
	 * @description 注册
	 * @router post /admin/register
	 * @request body registerRequest
	 */
	async index() {
		const ctx = this.ctx
		try {
			ctx.validate(rule, ctx.request.body)
			const status = await ctx.service.register.index(ctx.request.body)
			if (status === 'success') {
				ctx.helper.success('注册成功')
			} else {
				ctx.helper.error(status)
			}
		} catch (e) {
			ctx.helper.error(e)
		}
	}
}

module.exports = RegisterController
